package cn.jboa.dao.impl;

import cn.jboa.common.Constants;
import cn.jboa.dao.EmployeeDao;
import cn.jboa.entity.SysEmployee;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("empDao")
public class EmployeeDaoImpl extends BaseHibernateDaoSupport<SysEmployee> implements EmployeeDao {

    @Autowired
    public EmployeeDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }
    public SysEmployee fineEmployeeBySN(String sn) {
        String hql="from SysEmployee e where e.sn= ?";
        List<SysEmployee> result=this.getHibernateTemplate().find(hql,sn);

        return (result == null || result.size() == 0) ? null : result.get(0);
    }

    public SysEmployee getManager(SysEmployee employee) {
        String hql = "from SysEmployee e where e.status = ? and e.sysDepartment.id = ? and e.sysPosition.nameCn = ?";
        List<SysEmployee> emps = this.find(hql, Constants.EMPLOYEE_STAY, employee
                .getSysDepartment().getId(), Constants.POSITION_FM);
        if (emps == null || emps.size() == 0)
            return null;
        return emps.get(0);
    }
}
